﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace QuanLyKhachHang
{
    class SinhMa
    {
        /// <summary>
        /// Sinh mã tự động theo một quy ước
        /// </summary>
        /// <param name="DauNgu">Ký tự chữ</param>
        /// <param name="Bangcansinhma">DataTable chữa mã</param>
        /// <param name="format">Định dạng mã VD :  "00000"</param>
        /// <returns></returns>
        public static string Sinh(string DauNgu, System.Data.DataTable Bangcansinhma, String format)
        {
            int DauNgulength = DauNgu.Length;
            int thutu = 0;
            foreach (System.Data.DataRow row in Bangcansinhma.Rows)
            {
                try
                {
                    if (int.Parse(((row[0].ToString().Remove(DauNgulength) == DauNgu) ?
                        row[0].ToString().
                        Substring(DauNgulength) : "")) != thutu + 1)
                    {
                        break;
                    }
                    thutu = int.Parse(row[0].ToString().Substring(DauNgulength));
                }
                catch (FormatException) { };
            }
            thutu += 1;
            return (DauNgu + thutu.ToString(format));
        }

        public static System.Data.DataTable LayDuLieuGrid(System.Windows.Forms.DataGridView vGrid)
        {
            System.Data.DataTable tbl = new System.Data.DataTable();
            foreach (System.Windows.Forms.DataGridViewColumn c in vGrid.Columns)
                tbl.Columns.Add(new System.Data.DataColumn(c.Name));
            foreach (System.Windows.Forms.DataGridViewRow r in vGrid.Rows)
            {
                System.Data.DataRow row = tbl.NewRow();
                foreach (System.Data.DataColumn c in tbl.Columns)
                    row[c] = r.Cells[c.ColumnName].Value.ToString();
                tbl.Rows.Add(row);
            }
            return tbl;
        }

      }
}
